package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.anno.MyAnno;
import com.example.entity.Account;
import com.example.entity.Student;
import com.example.event.UserCreatedEvent;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
import com.example.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    private final StudentService studentService;

    private final ApplicationEventPublisher applicationEventPublisher;

    @Override
    @MyAnno
    public void testDS() {
        // 默认查询主数据源
        Account account = this.getById(1);
        System.out.println("===============" + account);

        // 查询 slave 数据源
        Student student = studentService.getById(1);
        System.out.println("===============" + student);

    }

    @Transactional
    @Override
    public void testSpringEvent() {
        // save User
        Account newUser = addNewUser();
        // send email
        // 注意这里其实是同步的
        applicationEventPublisher.publishEvent(new UserCreatedEvent().setUserId(newUser.getId()).setName(newUser.getName()));
        System.out.println("=====用户创建完毕=====");
    }

    /**
     * 新建一个用户
     */
    private Account addNewUser() {
        Account account = new Account().setName("newUser1").setCardNo(10010L).setGroupId("开发部").setPassword("123789");
        this.save(account);
        return account;
    }


    public static void main(String[] args) {
        String s = "hi你好棒🤞";
        System.out.println(s.length());

        String s2 = "你好😊";
        String s1 = s2.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
        System.out.println(s1);
    }
}
